package com.yyn.educenter.controller;


import com.yyn.R;
import com.yyn.educenter.entity.UcenterMember;
import com.yyn.educenter.entity.dto.RegisterDto;
import com.yyn.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2023-01-04
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/ucenter-member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;
    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
        String token = ucenterMemberService.login(ucenterMember);
        return R.success().data("token",token);
    }
    @ApiOperation("注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterDto registerDto){
        boolean register = ucenterMemberService.register(registerDto);
       if(register) return R.success().message("注册成功");
       return R.error().message("注册失败");
    }
    @ApiOperation("通过请求头的token获取信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){//通过请求头传值
        UcenterMember memberInfo = ucenterMemberService.getMemberInfo(request);
        return R.success().data("memberInfo",memberInfo);
    }
    @ApiOperation("直接通过token获取信息")
    @GetMapping("getMemberInfoByToken/{token}")
    public R getMemberInfoByToken(@PathVariable String token){//通过token传值
        UcenterMember memberInfo = ucenterMemberService.getMemberInfoByToken(token);
        return R.success().data("memberInfo",memberInfo);
    }
}

